#include <d_diag/diag.h>
/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-04-05     donghaomin       the first version
 */
#include "d_hardware.h"

d_bule_revcmd d_revbuffer;
UART_HandleTypeDef huart2;
unsigned char KEY1 = 0;

uint8_t aRxBuffer[1];

uint8_t temp1=0;
uint8_t USART_RX_BUF[USART_REC_LEN];   //接收缓冲
//接收状态位
//bit15接收完成标志
//bit14接收了0x0d
//bit13~0接收到的有效字节数目
uint16_t USART_RX_STA=0;//接收状态

/**
 * 使用uart2作为蓝牙通信端口
 * */
void Dong_MX_USART2_UART_Init(void)
{
  huart2.Instance = USART2;
  huart2.Init.BaudRate = 115200;
  huart2.Init.WordLength = UART_WORDLENGTH_8B;
  huart2.Init.StopBits = UART_STOPBITS_1;
  huart2.Init.Parity = UART_PARITY_NONE;
  huart2.Init.Mode = UART_MODE_TX_RX;
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart2) != HAL_OK)
  {
      rt_kprintf("uart2 init false !!!\n");
  }
  HAL_UART_Receive_IT(&huart2, (uint8_t *)&aRxBuffer, 1);
}

void Dong_MX_GPIO_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};

    /* GPIO Ports Clock Enable */
    __HAL_RCC_GPIOC_CLK_ENABLE();
    __HAL_RCC_GPIOA_CLK_ENABLE();

    /*Configure GPIO pin Output Level */
    HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);

    /*Configure GPIO pin : PC13 */
    // PC13 作为LED闪烁引脚
    GPIO_InitStruct.Pin = GPIO_PIN_13;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

    /*Configure GPIO pin : PA11 */
    // PA11 作为Key按键，下降沿触发
    GPIO_InitStruct.Pin = GPIO_PIN_11;
    GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    /* EXTI interrupt init*/
    HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);

}

/**
  * @brief This function handles EXTI line[15:10] interrupts.
  * 这个中断服务函数就是用于按键暂存数据的按键
  */
void EXTI15_10_IRQHandler(void)
{
  //  HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13); // 中断测试没问题
  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_11);
  KEY1 = 0x01;
}

/**
 * 本函数是接收uart数据的函数，
 * */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){
    if((USART_RX_STA&0x8000)==0)//接收未完成
    {
        if(USART_RX_STA&0x4000)//接收到了0x0d
        {
            if(aRxBuffer[0]!=0x0a)USART_RX_STA=0;//接收错误,重新开始
            else USART_RX_STA|=0x8000;  //接收完成了
        }
        else //还没收到0X0D
        {
            if(aRxBuffer[0]==0x0d)USART_RX_STA|=0x4000;
            else
            {
                USART_RX_BUF[USART_RX_STA&0X3FFF]=aRxBuffer[0] ;
                USART_RX_STA++;
                if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//接收数据错误,重新开始接收
            }
        }
    }
    HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
    HAL_UART_Receive_IT(&huart2, (uint8_t *)aRxBuffer, 1);
}
/**
  * @brief This function handles USART2 global interrupt.
  */
void USART2_IRQHandler(void)
{
    HAL_UART_IRQHandler(&huart2);
}

void Dong_USRT2_SendData(uint8_t *pData,uint32_t len) {
    HAL_UART_Transmit(&huart2,(uint8_t*)pData,len,1000); //发送
    while(__HAL_UART_GET_FLAG(&huart2,UART_FLAG_TC)!=SET);//等待发送完成
}



